/*******************
Input merchant-level data
Period: March-December 2004
*******************/

clear
clear matrix
clear mata

set more off

cd "Write the path of data folder"

use merchant.dta, clear

format shop %17.0g


label variable shop_transact "Total Transaction Quantity"

label variable shop_value "Total Transaction Value"

label variable shop_fee "Total Merchant Fee"


gen shop_rate = shop_fee/shop_value 
label variable shop_rate "Average Merchant Fee"

gen shop_avgvalue = shop_value/shop_transact
label variable shop_avgvalue "Average Transaction Value"


keep shop shop_transact shop_value shop_fee shop_rate shop_avgvalue

drop if shop_transact == . & shop_value == . &  shop_fee == . &  shop_rate == . &  shop_avgvalue == .  


/*******************
Convert the identifier to string format
Extract code 4-7 for area; 8-11 for industry
*******************/

gen code_s = string(shop,"%17.0g")


gen areacode_s = substr(code_s,-12,4)

gen areacode = real(areacode_s)

drop areacode_s

drop if areacode < 4400 | areacode > 4499

drop if areacode == 4400


gen code4_s = substr(code_s,-8,4)

sort code4_s code_s


/*******************
Define 4 and 87 groups
Assign fraud risk for 87 groups
********************/

do REStat_Define_groups


/*******************
Assign interchange fee and transfer fee
********************/

gen IFee = 0.014 if group5 == 1

replace IFee = 0.007 if group5 == 2

replace IFee = 0.007 if group5 == 3

replace IFee = 0.0035 if group5 == 4

replace IFee = 0 if group5 == 5


gen TFee = 0.002 if group5 == 1

replace TFee = 0.001 if group5 == 2

replace TFee = 0.001 if group5 == 3

replace TFee = 0.0005 if group5 == 4

replace TFee = 0 if group5 == 5


gen ITFee = IFee + TFee

gen shop_AFee = shop_rate - ITFee
label variable shop_AFee "Average Acquirer Fee"


/*******************
Match area data
********************/

sort areacode

merge areacode using area2004.dta

drop if _merge != 3

drop _merge


/*******************
Compute group measures
Imperfect match from MCC to merchant data

Recall: Opincome and Opcost are in Yuan
The other industry data is in 10000 Yuan
********************/

sort group87

merge group87 using industry2005.dta

drop if _merge != 3

drop _merge


/*******************
Count the number of shop in each industry 
Drop the industry with #shop < 4
********************/

gsort group87 -shop_value

by group87: egen group_transact = sum(shop_transact)

gen group_margin = (opincome-opcost)/opincome

gen group_LK = employee/totalasset 


gen one = 1

by group87: egen group_nshop = sum(one)

drop one

drop if group_nshop < 4


/*******************
Define varlables for regression
********************/

gen gdppc_1000 = GDPPC/1000

gen napopratio = NAPopulation/Population

gen WageEmployed_1000 = WageEmployed/1000

gen FIpc10000 = FI/Population 


/*******************
Regional FE
********************/

gen Chaozhou = 1 if Area=="Chaozhou"
replace Chaozhou = 0 if Chaozhou == .

gen Dongguan = 1 if Area=="Dongguan"
replace Dongguan = 0 if Dongguan == .

gen Foshan = 1 if Area=="Foshan"
replace Foshan = 0 if Foshan == .

gen Guangzhou = 1 if Area=="Guangzhou"
replace Guangzhou = 0 if Guangzhou == .

gen Heyuan = 1 if Area=="Heyuan"
replace Heyuan = 0 if Heyuan == .

gen Huizhou = 1 if Area=="Huizhou"
replace Huizhou = 0 if Huizhou == .

gen Jiangmen = 1 if Area=="Jiangmen"
replace Jiangmen = 0 if Jiangmen == .

gen Jieyang = 1 if Area=="Jieyang"
replace Jieyang = 0 if Jieyang == .

gen Maoming = 1 if Area=="Maoming"
replace Maoming = 0 if Maoming == .

gen Meizhou = 1 if Area=="Meizhou"
replace Meizhou = 0 if Meizhou == .

gen Qingyuan = 1 if Area=="Qingyuan"
replace Qingyuan = 0 if Qingyuan == .

gen Shanwei = 1 if Area=="Shanwei"
replace Shanwei = 0 if Shanwei == .

gen Shantou = 1 if Area=="Shantou"
replace Shantou = 0 if Shantou == .

gen Shaoguan = 1 if Area=="Shaoguan"
replace Shaoguan = 0 if Shaoguan == .

gen Yangjiang = 1 if Area=="Yangjiang"
replace Yangjiang = 0 if Yangjiang == .

gen Yunfu = 1 if Area=="Yunfu"
replace Yunfu = 0 if Yunfu == .

gen Zhanjiang = 1 if Area=="Zhanjiang"
replace Zhanjiang = 0 if Zhanjiang == .

gen Zhaoqing = 1 if Area=="Zhaoqing"
replace Zhaoqing = 0 if Zhaoqing == .

gen Zhongshan = 1 if Area=="Zhongshan"
replace Zhongshan = 0 if Zhongshan == .

gen Zhuhai = 1 if Area=="Zhuhai"
replace Zhuhai = 0 if Zhuhai == .


/*******************
Generate new group index
********************/

sort group87

egen temp = seq() if group87!=group87[_n-1]

by group87: egen group87s = mean(temp)

drop temp


/*******************
Generate IVs
1. The other shops in the same industry-area
2. The other shops in the same industry
********************/

sort group87s areacode

gen shop_transactshare = shop_transact/group_transact


by group87s areacode: egen temp_num = sum(shop_transactshare*shop_avgvalue)

by group87s areacode: egen temp_dem = sum(shop_transactshare)

gen rshop_VPT_T1 = (temp_num-shop_transactshare*shop_avgvalue)/(temp_dem-shop_transactshare)

drop temp_num temp_dem


by group87s: egen temp_num = sum(shop_transactshare*shop_avgvalue)

gen rshop_VPT_T2 = (temp_num-shop_transactshare*shop_avgvalue)/(1-shop_transactshare)

drop temp_num


/*******************
Working sample
Group5=3 loses a lot of data if shop_AFee > 0
  because of the upper limit for Apartment, Vehicle and Motor Home
Also, do not find wholesales industries with upper limit
********************/

drop if shop_AFee <= 0 

drop if group5 == 3

drop if rshop_VPT_T1 == .

* There are 68 observations with AFee <= 0.0000238, i.e. 0.00238%
* For 0.5% fee, AFee is about 0.05%; 
drop if shop_AFee < 0.0009 


/***********************
Variable construction
***********************/

gen Cater = 1 if group5 == 1
replace Cater = 0 if Cater ==.

gen Wel_Pub = 1 if group5 == 4 | group5 == 5
replace Wel_Pub = 0 if Wel_Pub ==.


gen VPT = shop_avgvalue

gen VPT_AFee = shop_avgvalue*shop_AFee

gen VPT_ITFee = shop_avgvalue*ITFee

gen VPT_Margin = VPT*group_margin

gen VPT_LK = VPT*group_LK


gen VPT_Cater = shop_avgvalue*Cater

gen VPT_Wel_Pub = shop_avgvalue*Wel_Pub


gen VPT_Chaozhou = VPT*Chaozhou 
gen VPT_Dongguan = VPT*Dongguan
gen VPT_Foshan = VPT*Foshan 
gen VPT_Guangzhou = VPT*Guangzhou 
gen VPT_Heyuan = VPT*Heyuan 
gen VPT_Huizhou = VPT*Huizhou 
gen VPT_Jiangmen = VPT*Jiangmen 
gen VPT_Jieyang = VPT*Jieyang 
gen VPT_Maoming = VPT*Maoming 
gen VPT_Meizhou = VPT*Meizhou
gen VPT_Qingyuan = VPT*Qingyuan 
gen VPT_Shantou = VPT*Shantou
gen VPT_Shanwei = VPT*Shanwei
gen VPT_Shaoguan = VPT*Shaoguan
gen VPT_Yangjiang = VPT*Yangjiang
gen VPT_Yunfu = VPT*Yunfu
gen VPT_Zhanjiang = VPT*Zhanjiang
gen VPT_Zhaoqing = VPT*Zhaoqing
gen VPT_Zhongshan = VPT*Zhongshan
gen VPT_Zhuhai = VPT*Zhuhai


/***********************
IV construction
***********************/

gen rVPT_ITFee = rshop_VPT_T1*ITFee

gen rVPT_Margin = rshop_VPT_T1*group_margin

gen rVPT_LK = rshop_VPT_T1*group_LK


gen rVPT_Cater = rshop_VPT_T1*Cater

gen rVPT_Wel_Pub = rshop_VPT_T1*Wel_Pub


gen rVPT_Chaozhou = rshop_VPT_T1*Chaozhou 
gen rVPT_Dongguan = rshop_VPT_T1*Dongguan
gen rVPT_Foshan = rshop_VPT_T1*Foshan 
gen rVPT_Guangzhou = rshop_VPT_T1*Guangzhou 
gen rVPT_Heyuan = rshop_VPT_T1*Heyuan 
gen rVPT_Huizhou = rshop_VPT_T1*Huizhou 
gen rVPT_Jiangmen = rshop_VPT_T1*Jiangmen 
gen rVPT_Jieyang = rshop_VPT_T1*Jieyang 
gen rVPT_Maoming = rshop_VPT_T1*Maoming 
gen rVPT_Meizhou = rshop_VPT_T1*Meizhou
gen rVPT_Qingyuan = rshop_VPT_T1*Qingyuan 
gen rVPT_Shantou = rshop_VPT_T1*Shantou
gen rVPT_Shanwei = rshop_VPT_T1*Shanwei
gen rVPT_Shaoguan = rshop_VPT_T1*Shaoguan
gen rVPT_Yangjiang = rshop_VPT_T1*Yangjiang
gen rVPT_Yunfu = rshop_VPT_T1*Yunfu
gen rVPT_Zhanjiang = rshop_VPT_T1*Zhanjiang
gen rVPT_Zhaoqing = rshop_VPT_T1*Zhaoqing
gen rVPT_Zhongshan = rshop_VPT_T1*Zhongshan
gen rVPT_Zhuhai = rshop_VPT_T1*Zhuhai


/***********************
Table 5: IV Model with City FEs
24 Parameters of surplus: Margin, LK, 2 Group FE, 20 City FE 
3 Parameters of bargain: LK, 2 Group FE 
1 Parameter of cost

24 Moments of surplus: Margin x rVPT, LK x rVPT, 2 Group FE x rVPT, 20 City FE x rVPT
3 Moments of bargain: LK, 2 Group FE
2 Moments of cost: Constant, FIpc10000

One extra moment 
***********************/

* Column 2

gmm ( VPT_AFee*(1+exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub)) - {b_Cost}*exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub) + VPT_ITFee ///
- {AM_Margin}*VPT_Margin - {AM_LK}*VPT_LK  - {AM1}*VPT_Cater - {AM45}*VPT_Wel_Pub - ({AMr:VPT_Chaozhou VPT_Dongguan VPT_Foshan VPT_Guangzhou VPT_Heyuan VPT_Huizhou VPT_Jiangmen VPT_Jieyang VPT_Maoming VPT_Meizhou ///
VPT_Qingyuan VPT_Shantou VPT_Shanwei VPT_Shaoguan VPT_Yangjiang VPT_Yunfu VPT_Zhanjiang VPT_Zhaoqing VPT_Zhongshan VPT_Zhuhai}) ), ///
instruments(FIpc10000 rVPT_Margin rVPT_LK rVPT_Cater rVPT_Wel_Pub group_LK Cater Wel_Pub ///
rVPT_Chaozhou rVPT_Dongguan rVPT_Foshan rVPT_Guangzhou rVPT_Heyuan rVPT_Huizhou rVPT_Jiangmen rVPT_Jieyang rVPT_Maoming rVPT_Meizhou ///
rVPT_Qingyuan rVPT_Shantou rVPT_Shanwei rVPT_Shaoguan rVPT_Yangjiang rVPT_Yunfu rVPT_Zhanjiang rVPT_Zhaoqing rVPT_Zhongshan rVPT_Zhuhai) conv_maxiter(400) from(b_LK 5 b1 3 b45 -2 b_Cost 3)

estat overid


gen AM_J = _b[AM_Margin:_cons]*group_margin + _b[AM_LK:_con]*group_LK + _b[AM1:_cons]*Cater + _b[AM45:_cons]*Wel_Pub

gen AM_R = _b[AMr:VPT_Chaozhou]*Chaozhou + _b[AMr:VPT_Dongguan]*Dongguan + _b[AMr:VPT_Foshan]*Foshan + _b[AMr:VPT_Guangzhou]*Guangzhou ///
+ _b[AMr:VPT_Heyuan]*Heyuan + _b[AMr:VPT_Huizhou]*Huizhou + _b[AMr:VPT_Jiangmen]*Jiangmen + _b[AMr:VPT_Jieyang]*Jieyang ///
+ _b[AMr:VPT_Maoming]*Maoming + _b[AMr:VPT_Meizhou]*Meizhou + _b[AMr:VPT_Qingyuan]*Qingyuan + _b[AMr:VPT_Shantou]*Shantou ///
+ _b[AMr:VPT_Shanwei]*Shanwei  + _b[AMr:VPT_Shaoguan]*Shaoguan + _b[AMr:VPT_Yangjiang]*Yangjiang + _b[AMr:VPT_Yunfu]*Yunfu ///
+ _b[AMr:VPT_Zhanjiang]*Zhanjiang + _b[AMr:VPT_Zhaoqing]*Zhaoqing  + _b[AMr:VPT_Zhongshan]*Zhongshan + _b[AMr:VPT_Zhuhai]*Zhuhai

gen AM = AM_J + AM_R


gen bargain_acquirer = 1/(1+exp(_b[b_LK:_cons]*group_LK +_b[b1:_cons]*Cater + _b[b45:_cons]*Wel_Pub))


predict res, residuals

gen res_adj = res*bargain_acquirer/((1-bargain_acquirer)*_b[b_Cost:_con])

gen cost_adj =_b[b_Cost:_con]*(1+res_adj)/VPT


/***********************
Figure 1M: Counterfactual 1 - Rising card penetration
***********************/

gen Surplus_A = bargain_acquirer*(AM - ITFee)

gen Surplus_M = (1-bargain_acquirer)*cost_adj

gen AFee_hat = Surplus_M + Surplus_A 

gen AM_INF =  AM - ITFee



gen AM_INF_2 = 1.05*AM - ITFee
gen Surplus_A_hat2 = bargain_acquirer*AM_INF_2
gen AFee_hat2 = Surplus_M + Surplus_A_hat2

gen AM_INF_3 = 1.1*AM - ITFee
gen Surplus_A_hat3 = bargain_acquirer*AM_INF_3
gen AFee_hat3 = Surplus_M + Surplus_A_hat3

gen AM_INF_4 = 1.15*AM - ITFee
gen Surplus_A_hat4 = bargain_acquirer*AM_INF_4
gen AFee_hat4 = Surplus_M + Surplus_A_hat4

gen AM_INF_5 = 1.2*AM - ITFee
gen Surplus_A_hat5 = bargain_acquirer*AM_INF_5
gen AFee_hat5 = Surplus_M + Surplus_A_hat5

gen AM_INF_6 = 1.25*AM - ITFee
gen Surplus_A_hat6 = bargain_acquirer*AM_INF_6
gen AFee_hat6 = Surplus_M + Surplus_A_hat6

gen AM_INF_7 = 1.3*AM - ITFee
gen Surplus_A_hat7 = bargain_acquirer*AM_INF_7
gen AFee_hat7 = Surplus_M + Surplus_A_hat7

gen AM_INF_8 = 1.35*AM - ITFee
gen Surplus_A_hat8 = bargain_acquirer*AM_INF_8
gen AFee_hat8 = Surplus_M + Surplus_A_hat8

gen AM_INF_9 = 1.4*AM - ITFee
gen Surplus_A_hat9 = bargain_acquirer*AM_INF_9
gen AFee_hat9 = Surplus_M + Surplus_A_hat9

gen AM_INF_10 = 1.45*AM - ITFee
gen Surplus_A_hat10 = bargain_acquirer*AM_INF_10
gen AFee_hat10 = Surplus_M + Surplus_A_hat10

gen AM_INF_11 = 1.5*AM - ITFee
gen Surplus_A_hat11 = bargain_acquirer*AM_INF_11
gen AFee_hat11 = Surplus_M + Surplus_A_hat11



gen surplus_t1 = 1.5*AM - ITFee - cost_adj

gen surplus_m1 = 1.5*AM - ITFee - AFee_hat11

gen surplus_a1 = AFee_hat11 - cost_adj


bys group5: su AFee_hat*


/***********************
Figure 1L: Counterfactual 2 - The Role of Bargaining Power
***********************/

gen AFee_hat_100 = (1-bargain_acquirer)*cost_adj + bargain_acquirer*(AM - ITFee)

gen AFee_hat_098 = (1-0.98*bargain_acquirer)*cost_adj + 0.98*bargain_acquirer*(AM - ITFee) 

gen AFee_hat_096 = (1-0.96*bargain_acquirer)*cost_adj + 0.96*bargain_acquirer*(AM - ITFee)

gen AFee_hat_094 = (1-0.94*bargain_acquirer)*cost_adj + 0.94*bargain_acquirer*(AM - ITFee)

gen AFee_hat_092 = (1-0.92*bargain_acquirer)*cost_adj + 0.92*bargain_acquirer*(AM - ITFee)

gen AFee_hat_090 = (1-0.90*bargain_acquirer)*cost_adj + 0.90*bargain_acquirer*(AM - ITFee)

gen AFee_hat_088 = (1-0.88*bargain_acquirer)*cost_adj + 0.88*bargain_acquirer*(AM - ITFee)

gen AFee_hat_086 = (1-0.86*bargain_acquirer)*cost_adj + 0.86*bargain_acquirer*(AM - ITFee)

gen AFee_hat_084 = (1-0.84*bargain_acquirer)*cost_adj + 0.84*bargain_acquirer*(AM - ITFee)

gen AFee_hat_082 = (1-0.82*bargain_acquirer)*cost_adj + 0.82*bargain_acquirer*(AM - ITFee)

gen AFee_hat_080 = (1-0.80*bargain_acquirer)*cost_adj + 0.80*bargain_acquirer*(AM - ITFee) 


bys group5: su AFee_hat_100-AFee_hat_080
